import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/product_userpostratio_odoo_api.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/models/odoo/product_userpostratio.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import '../../../generated/l10n.dart';

class ProductUserpostratioEdit extends StatefulWidget {
  ProductUserpostratioEdit({this.productUserpostratio});

  final ProductUserpostratio? productUserpostratio;

  @override
  _ProductUserpostratioEditState createState() =>
      _ProductUserpostratioEditState();
}

class _ProductUserpostratioEditState extends State<ProductUserpostratioEdit> {
  late ProductUserpostratio productUserpostratio;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  //工厂
  List<SelectOptionVO> factoryList = [];
  //车间
  List<SelectOptionVO> workshopList = [];
  //工位
  List<SelectOptionVO> stationList = [];
  //启用
  List<SelectOptionVO> actionList = [];
  //岗位
  List<SelectOptionVO> jobList = [];

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.productUserpostratio == null;
    this.productUserpostratio =
        widget.productUserpostratio ?? ProductUserpostratio();
    // ignore: unnecessary_statements
    this.productUserpostratio.active ??= "1";
    _loadSelectVO();
  }

  _loadSelectVO() async {
    factoryList = await DynamicDataApi.getModelSelect(
        model: "hr.department",
        name: "complete_name",
        key: "parent_id",
        operator: "=",
        val: null);
    if (productUserpostratio.factoryId != null) {
      workshopList = await DynamicDataApi.getModelSelect(
          model: "hr.department",
          name: "complete_name",
          key: "parent_id",
          operator: "=",
          val: int.parse(productUserpostratio.factoryId!));
      if (productUserpostratio.workshopId != null) {
        stationList = await DynamicDataApi.getModelSelect(
            model: "base.productionstation",
            name: "stationname",
            key: 'workshop_id',
            val: int.parse(productUserpostratio.workshopId!));
        jobList = await DynamicDataApi.getModelSelect(
            model: "hr.job",
            name: "name",
            key: "department_id",
            active: false,
            operator: "=",
            val: int.parse(productUserpostratio.workshopId!));
      }
    }

    actionList = [
      SelectOptionVO(value: '0', label: "禁用"),
      SelectOptionVO(value: '1', label: "启用")
    ];
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelect(
            label: '生产工厂',
            dataList: factoryList,
            value: productUserpostratio.factoryId,
            onSaved: (v) {
              productUserpostratio.factoryId = v;
            },
            onChange: (v) async {
              if (productUserpostratio.factoryId != v) {
                formKey.currentState!.save();
                productUserpostratio.factoryId = v;
                productUserpostratio.workshopId = null;
                productUserpostratio.stationId = null;
                productUserpostratio.postId = null;
                stationList = [];
                jobList = [];
                try {
                  workshopList = await DynamicDataApi.getModelSelect(
                      model: "hr.department",
                      name: "complete_name",
                      key: "parent_id",
                      operator: "=",
                      val: int.parse(v));
                } catch (e) {
                  workshopList = [];
                }
                setState(() {});
              }
            },
          ),
          CrySelect(
            label: '生产车间',
            dataList: workshopList,
            value: productUserpostratio.workshopId,
            onSaved: (v) {
              productUserpostratio.workshopId = v;
            },
            onChange: (v) async {
              if (productUserpostratio.workshopId != v) {
                formKey.currentState!.save();
                productUserpostratio.workshopId = v;
                productUserpostratio.stationId = null;
                productUserpostratio.postId = null;
                try {
                  stationList = await DynamicDataApi.getModelSelect(
                      model: "base.productionstation",
                      name: "stationname",
                      key: 'workshop_id',
                      val: int.parse(v));
                  jobList = await DynamicDataApi.getModelSelect(
                      model: "hr.job",
                      name: "name",
                      key: "department_id",
                      active: false,
                      operator: "=",
                      val: int.parse(v));
                } catch (e) {
                  stationList = [];
                  jobList = [];
                }
                setState(() {});
              }
            },
          ),
          CrySelect(
            label: '设备工位',
            dataList: stationList,
            value: productUserpostratio.stationId,
            onSaved: (v) {
              productUserpostratio.stationId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CrySelect(
            label: '岗位名称',
            dataList: jobList,
            value: productUserpostratio.postId,
            onSaved: (v) {
              productUserpostratio.postId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '岗位系数',
            value: productUserpostratio.postratio,
            onSaved: (v) {
              productUserpostratio.postratio = v;
            },
            validator: (v) {
              if (v!.isEmpty) {
                return null;
              } else if (double.parse(v) is double) {
                return null;
              } else if (int.parse(v) is int) {
                return null;
              } else {
                return '请输入数字';
              }
            },
          ),
          CryInput(
            label: '备注',
            value: productUserpostratio.remark,
            onSaved: (v) {
              productUserpostratio.remark = v;
            },
          ),
          CrySelect(
            label: '启用状态',
            dataList: actionList,
            value: productUserpostratio.active,
            onSaved: (v) {
              productUserpostratio.active = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            ProductUserpostratioOdooApi.saveOrUpdate(
                    productUserpostratio.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
